package com.warm.base.jdk.thread.realize;

import lombok.Data;

/**
 * @Date: 2020/4/2 13:28
 * @Author: LChuang
 * @Description:
 */
@Data
public class VolatileKeyword implements Runnable{



    private volatile Boolean flag = false;

    @Override
    public void run() {
        while (true) {
            if (flag) {
                System.out.println(Thread.currentThread().getName()+" : "+flag);
                break;
            }
        }
    }

    public static void main(String[] args) {

        try {
            VolatileKeyword unVolatile = new VolatileKeyword();
            Thread thread = new Thread(unVolatile);
            thread.start();

            //给线程启动的时间
            Thread.sleep(500);

            //更新flay
            unVolatile.flag = true;
            System.out.println("flag is true, thread should print");
            Thread.sleep(1000);
            System.out.println("I have slept 1 seconds. Is there anything printed ?");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
